#region Imported Namespaces


using System;
using System.Diagnostics;

using WtfManager.Framework;


#endregion


namespace WtfManager.HierarchicalData
{
  ///<summary>
  /// Manages hierarchical data.
  ///</summary>
  public class DataHierarchyService: ExtensibleService
  {
    #region Fields


    private Hierarchy _hierarchy;


    #endregion


    #region Properties/Indexers


    /// <summary>
    /// Gets a description of a service.
    /// </summary>
    public override string Description
    {
      get
      {
        return "Manages hierarchical data.";
      }
    }


    ///<summary>
    /// Gets a collection of hierarchies.
    ///</summary>
    public Hierarchy Hierarchy
    {
      get
      {
        Debug.Assert(IsInitialized, "DataHierarchyService is not yet initialized.");

        return _hierarchy;
      }
    }


    /// <summary>
    /// Gets a name of a service.
    /// </summary>
    public override string Name
    {
      get
      {
        return "Data Hierarchy";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
    }


    protected override FrameworkComponentState OnLoad()
    {
      _hierarchy = new Hierarchy();

      return FrameworkComponentState.Loaded;
    }


    protected override void OnUnload()
    {
      _hierarchy = null;
    }


    ///<summary>
    /// Called when extension was added.
    ///</summary>
    protected override void OnExtensionAdded(Service extension)
    {
      if (IsInitialized)
      {
        var hierarchyExtension = (IHierarchyExtension)extension;
        _hierarchy.AddNode(hierarchyExtension.Hierarchy);
      }
    }


    ///<summary>
    /// Determines whether a specified extension is valid.
    ///</summary>
    ///<param name="extension">An extension to check.</param>
    public override bool IsExtension(Service extension)
    {
      return extension is IHierarchyExtension;
    }


    ///<summary>
    /// Called when an extension was removed.
    ///</summary>
    protected override void OnExtensionRemoved(Service extension)
    {
      var hierarchyExtension = (IHierarchyExtension)extension;
      _hierarchy.RemoveNode(hierarchyExtension.Hierarchy);
    }


    protected override FrameworkComponentState OnInitialize()
    {
      foreach (Service extension in Extensions)
      {
        if (extension.IsInitialized)
        {
          var hierarchyExtension = (IHierarchyExtension)extension;
          _hierarchy.AddNode(hierarchyExtension.Hierarchy);
        }
        else
        {
          extension.StateChanged += ExtensionStateChanged;
        }
      }

      return FrameworkComponentState.Initialized;
    }


    private void ExtensionStateChanged(object sender, EventArgs e)
    {
      var extension = (Service)sender;
      var hierarchyExtension = (IHierarchyExtension)extension;
      TreeItem hierarchy = hierarchyExtension.Hierarchy;

      if (extension.IsInitialized)
      {
        _hierarchy.AddNode(hierarchy);
      }
      else
      {
        if (hierarchy != null && _hierarchy.Children.Contains(hierarchy))
        {
          _hierarchy.RemoveNode(hierarchy);
        }
      }
    }


    #endregion
  }
}